package kotan.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class BinaryUtil {

    public static byte[] read(File file) {
        if (!file.exists() || !file.canRead()) return null;
        ByteArrayOutputStream output = null;
        FileInputStream input = null;
        try {
            output = new ByteArrayOutputStream(1024 * 1024 * 10);
            input = new FileInputStream(file);
            byte[] buf = new byte[10 * 1024];
            while (true) {
                int len = input.read(buf);
                if (len == -1) break;
                output.write(buf, 0, len);
            }
            output.flush();
            return output.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (input != null) input.close();
            } catch (IOException e) {
                // do nothing.
            }
            try {
                if (output != null) output.close();
            } catch (IOException e) {
                // do nothing.
            }
        }
    }
}
